package org.jside.cdn;



import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.xidea.lite.parser.ParseContext;
import org.xidea.lite.parser.impl.ParseContextImpl;
import org.xidea.lite.servlet.ServletTemplateEngine;
import org.xidea.lite.servlet.TemplateServlet;

/**
 * JSICDN,通过URL 按需装载脚本库
 * path:/scripts/;org.jside:Tween+org.jside:format+org.xidea.test.*
 * 
 * @author jindw
 */
public class CDNTestServlet extends TemplateServlet {
	@Override
	public void init(final ServletConfig config) throws ServletException {
		super.init(config);
		templateEngine = new ServletTemplateEngine(config){
			protected ParseContext createParseContext() {
				try {
					ParseContextImpl context = new ParseContextImpl(getResource("/"), featrues, null, null);
					context.addNodeParser(new CDNOptimizeParser());
					return context;
				
				} catch (MalformedURLException e) {
					throw new RuntimeException(e);
				}
			}
		};
	}

	@Override
	protected Map<Object, Object> createModel(HttpServletRequest req) {
		Map<Object, Object> model = super.createModel(req);
		String token = CDNServlet.getDebugTokenValue(this.getServletContext());
		model.put(CDNServlet.CDN_DEBUG_TOKEN_NAME,token);
		Map<Object, Object> params = req.getParameterMap();
		model.put("params", params);
		Map<Object, Object> param = new HashMap<Object, Object>();
		for(Object k : params.keySet()){
			param.put(k,req.getParameter((String)k));
		}
		model.put("param", param);
		return model;
	}

}
